<?php
namespace Index\Controller;
use Think\Controller;
class SearchController extends Controller {
    public function indexAction(){
    	$wd = I('get.keyword');
    	$num = 5;
		import("Vendor.Pscws.pscws4");
		$pscws = new \PSCWS4();
		$pscws->PSCWS4('utf8');
//		 var_dump(CONF_PATH);
	    $pscws->set_dict(CONF_PATH . 'etc/dict.utf8.xdb');
		$pscws->set_rule(CONF_PATH . 'etc/rules.utf8.ini');
		$pscws->set_ignore(true);
		$pscws->send_text($wd);
		$words = $pscws->get_tops($num);
		$pscws->close();

		$sql = "select t1.attention,t1.reply,t1.id,t1.uid,t1.title,t1.views,t1.create_time,t3.username,t3.face_url,t2.category_name from question t1 inner join category t2 on t2.id = t1.cid inner join user t3 on t1.uid = t3.id where t1.";

//		$sql = "select t1.attention,t1.reply,t1.id,t1.uid,t1.title,t1.views,t1.create_time,t2.username,t2.face_url,t3.category_name from question t1 left join user t2 on t1.uid = t2.id left join category t3 on t3.id = t1.cid order by t1.create_time desc limit {$Page->firstRow},{$Page->listRows}";

//		 $tags = array();
		foreach($words as $val) {
			$sql .= "title like "."'%".$val['word']."%' or ";
		}
		$model = M();
		if(empty($words)){
			$resData = array();
		}else{
			$sql = substr($sql,0,-4);
			$resData = $model->query($sql);
			$count = count($resData);
			// $Page = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(1)

			// $show = $Page->show();// 分页显示输出
			// $this->assign('page',$show);
			// $sql.= "order by create_time desc limit {$Page->firstRow},{$Page->listRows}";
			// $resData = $model->query($sql);
		}



		$this->assign('resData',$resData);

		// return implode(',', $tags);


		//热门话题
		$sql = "select t1.id,t1.uid,t1.title,t1.attention,t2.face_url from question t1 left join user t2 on t1.uid = t2.id order by attention desc limit 5";
		$popQuestion = $model->query($sql);
		$this->assign('popQuestion',$popQuestion);

		//热门用户
		$sql = "select count(*) cnt,t1.uid,t2.username,t2.face_url from answer t1 left join user t2 on t1.uid = t2.id group by t1.uid order by cnt desc limit 5";
		$maxAnswer = $model->query($sql);
		$this->assign('maxAnswer',$maxAnswer);

		$this->display('index');
    }
}